//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <ddk.h>

#define THREADAMOUNT        100

Int32 FinishedThreadtest01(void *pvPara)
{
    IThread         *pIThread;
    ThreadState     thdState;
    ECode         ec;

    ec = DzGetCurrentThread(&pIThread);
    assert(NOERROR == ec);

    ec = pIThread->GetState(&thdState);
    assert(NOERROR == ec);
    assert(ThreadState_Running == thdState);

    DzYield();

    pIThread->Release();

    return NOERROR;
}

EXTERN void AbortFinishedThread()
{
    IThread     *pIThread[THREADAMOUNT];
    ThreadState thdState;
    ECode     ec;
    uint_t      i;

    for (i = 0; i < THREADAMOUNT; i++) {
        ec = DzCreateKernelThread(FinishedThreadtest01, (void *)0, 0,
                        (ThreadPriorityRank)0xFFFFEEEE, &pIThread[i]);

        if (FAILED(ec)) {
            kprintf("Create thread %d failed, the ec = 0x%08x\n", i, ec);
            assert(0);

            return;
        }

        DzYield();
    }

    for (i = 0; i < THREADAMOUNT; i++) {
        ec = pIThread[i]->Join(INFINITE);
        assert(NOERROR == ec);
    }

    for (i = 0; i < THREADAMOUNT; i++) {
        ec = pIThread[i]->GetState(&thdState);
        assert(NOERROR == ec);
        assert(ThreadState_Finished == thdState);
    }

    for (i = 0; i < THREADAMOUNT; i++) {
        ec = pIThread[i]->Abort();
        assert(S_FALSE == ec);
    }

    for (i = 0; i < THREADAMOUNT; i++) {
        ec = pIThread[i]->GetState(&thdState);
        assert(NOERROR == ec);
        assert(ThreadState_Finished == thdState);
    }

    for (i = 0; i < THREADAMOUNT; i++) {
        pIThread[i]->Release();
    }
}
